package deployr.cli;

import deployr.core.*;
import deployr.domain.*;

public class CreateUpdateWarResponder extends Responder {

	private final Projects projects;
	private final Workspace workspace;

	public CreateUpdateWarResponder(Workspace workspace, Projects projects) {
		this.workspace = workspace;
		this.projects = projects;
	}
	
	@Override
	protected boolean shouldRespond(String method, String requestURI) {
		return "POST".equals(method) && requestURI.matches(".*/updates/.*/war.*");
	}	

	@Override
	protected Result respondTo(String method, String requestURI, Input input) {

		String updateKey = requestURI.replaceAll(".*/updates/", "").replaceAll("/war.*", "");
		IUpdate update = workspace.findUpdate(updateKey);
		if (notFound(update)) return new Result(404);

		workspace.deploy(update);

		return new Result(200);
	}

}
